<?

//require_once("class_PgMonSQL.php");
//require_once("classes/class_Hosts.php");

require_once("classes/class_Navi.php");

//require_once("classes/class_WUHostCluster.php");

//require_once("forms.php");
//require_once("list.php");

define("CLUSTER_LIST",1);
define("CLUSTER_ADD",2);
define("CLUSTER_EDIT",3);
define("CLUSTER_DISCOVER",4);
define("CLUSTER_WIEV",5);



$menu=new Navi();
echo "<div style=\"background-color: #DDD\"><b><a href=".$_SERVER['PHP_SELF']."?action=cluster_add>Add New Host</a></b>";



if (isset($_GET['action']) && $_GET['action'] == 'cluster_add') {
    $menu->navi(CLUSTER_ADD);
    WUHostCluster::cluster_add_edit_form();
}

if (isset($_GET['action']) && $_GET['action']=='cluster_view') {
    $hc=new WUHostCluster($_GET['hc_id']);
    $menu->set_active_object(CLUSTER_WIEV,$hc->get_id(),$hc->get_menu_info());
    $menu->navi(CLUSTER_VIEW);
//    $hc->view_information();
}

/*

if (isset($_GET['action'])) {
// ************************ List Content ****************************
    if ($_GET['action']=='list') {
	echo "<center>Configuration Page for PostgreSQL clusters</center></br>
	<div style=\"background-color: #DDD\">Cluster: </b></div><br>";
	if ($_GET['content']=='host_cluster') {
	    list_clusters_conf();
	} elseif ($_GET['content']=='database_name') {
	    list_cluster_databases($_GET['hc_id']);
	} elseif ($_GET['content']=='schema_name') {
	    list_database_schemas($_GET['dn_id']);
	}
// ************************ Add Content *****************************
    } elseif ($_GET['action']=='add') {
	if ($_GET['content']=='host_cluster') {
	    host_cluster_form();
	}
// ************************ Edit Content ****************************
    } elseif ($_GET['action']=='edit') {
	if ($_GET['content']=='host_cluster') {
	    host_cluster_form($_GET['hc_id']);
	} elseif ($_GET['content']=='database_name') {
	    edit_database_form($_GET['dn_id']);
	} elseif ($_GET['content']=='schema_name') {
	    edit_schema_form($_GET['sn_id']);
	}
// ************************ Delete Cluster **************************
    } elseif ($_GET['action']=='remove') {
	if ($_GET['content']=='host_cluster') {
	    remove_cluster_warn($_GET['hc_id']);
	}
    } elseif ($_GET['action']=='destroy') {
	if ($_GET['content']=='host_cluster') {
	    $hc=new HostCluster($_GET['hc_id']);
	    $hc->set_field('alive','f');
	    $hc->save();
	    list_clusters_conf();
	}
// ************************ Discover Content ************************
    } elseif ($_GET['action']=='discover') {
	if ($_GET['content']=='host_cluster') {
	    $hc=new HostCluster($_GET['hc_id']);
	    list_cluster_databases($_GET['hc_id'],$hc->discover());
	} elseif ($_GET['content']=='database_name') {
	    $sn=new DatabaseName($_GET['dn_id']);
	    list_database_schemas($_GET['dn_id'],$sn->discover());
	}
// **************************************************************
// ************************ Stat Content ************************
    } elseif ($_GET['action']=='stat') {
	echo "<center>Statistic Page for PostgreSQL clusters</center></br>
	<div style=\"background-color: #DDD\">Cluster: </b></div><br>";
	if ($_GET['content']=='host_cluster') {
	    list_clusters_stat();
	} elseif ($_GET['content']=='database_name') {
	    list_databases_stat($_GET['hc_id']);
	}
    }
}




*/

//if (isset($_POST['add_cluster_submit'])) {
function add_cluster() {
    $hc=new HostCluster;

    $hc->set_field('hostname',$_POST['add_cluster_hostname']);
    $hc->set_field('ip_address',$_POST['add_cluster_ip_address']);
    $hc->set_field('is_master',$_POST['add_cluster_is_master']);

    $hc->set_field('alive','t');

    if (isset($_POST['add_cluster_fqdn'])) {
        $hc->set_field('fqdn',$_POST['add_cluster_fqdn']);
    }
    if (isset($_POST['add_cluster_spec_comments'])) {
        $hc->set_field('spec_comments',$_POST['add_cluster_spec_comments']);
    }
// *****************
    $conn_param="{";
    $conn_param.="hostaddr=".$_POST['add_cluster_ip_address'].",";
    $conn_param.="dbname=".($_POST['add_cluster_conn_dbname'] ? $_POST['add_cluster_conn_dbname'] : 'postgres').",";
    $conn_param.="port=".($_POST['add_cluster_conn_port'] ? $_POST['add_cluster_conn_port'] : '5432').",";
    $conn_param.="user=".($_POST['add_cluster_conn_user'] ? $_POST['add_cluster_conn_user'] : 'postgres').",";
    if ($_POST['add_cluster_conn_password']) {
        $conn_param.="password=".$_POST['add_cluster_conn_password'].",";
    }
    $conn_param.="sslmode=".$_POST['add_cluster_conn_sslmode']."}";
    $hc->set_field('conn_param',$conn_param);
    $hc->save();
}
/*
if (isset($_POST['edit_cluster_submit'])) {
    $hc=new HostCluster($_POST['edit_cluster_id']);
    $hc->update_cluster($_POST);
    list_host_clusters();
}

if (isset($_POST['edit_dn__submit'])) {
    $dn=new DatabaseName($_POST['edit_dn__dn_id']);
    $dn->update_database($_POST);
    list_cluster_databases($_POST['edit_dn__hc_id']);
}

if (isset($_POST['edit_sn__submit'])) {
    $sn=new SchemaName($_POST['edit_sn__sn_id']);
    $sn->update_schema($_POST);
    list_database_schemas($_POST['edit_sn__dn_id']);

}
*/
?>
